package chapter3.lc209;

//LC 209 长度最小的子数组

/**
 * 滑动窗口
 */
public class MinimumSizeSubarraySum {
    public int minSubArrayLen(int target, int[] nums) {
        int sum = 0;
        int res = nums.length + 1;
        for (int i = 0, j = -1; i < nums.length;) {
            if (sum < target && j < nums.length -1) {
                sum += nums[j+1];
                j++;
            }else {
                sum -= nums[i];
                i++;
            }
            if (sum >= target) {
                res = Math.min(res, j - i + 1);
            }
        }
        if (res == nums.length + 1) {
            res = 0;
        }
        return res;
    }
}
